// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ExpandableQuery.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.LinqKit
{
    #region Usings

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;

    #endregion

    /// <summary>
    /// An IQueryable wrapper that allows us to visit the query's expression tree just before LINQ to SQL gets to it.
    /// This is based on the excellent work of Tomas Petricek: http://tomasp.net/blog/linq-expand.aspx
    /// </summary>
    /// <typeparam name="T">
    /// </typeparam>
    public class ExpandableQuery<T> : IQueryable<T>, IOrderedQueryable<T>, IOrderedQueryable
    {
        #region Constants and Fields

        /// <summary>
        /// The inner.
        /// </summary>
        private readonly IQueryable<T> inner;

        /// <summary>
        /// The provider.
        /// </summary>
        private readonly ExpandableQueryProvider<T> provider;

        #endregion

        // Original query, that we're wrapping
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ExpandableQuery{T}"/> class.
        /// </summary>
        /// <param name="inner">
        /// The inner.
        /// </param>
        internal ExpandableQuery(IQueryable<T> inner)
        {
            this.inner = inner;
            this.provider = new ExpandableQueryProvider<T>(this);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets IQueryable.ElementType.
        /// </summary>
        Type IQueryable.ElementType
        {
            get
            {
                return typeof(T);
            }
        }

        /// <summary>
        /// Gets IQueryable.Expression.
        /// </summary>
        Expression IQueryable.Expression
        {
            get
            {
                return this.inner.Expression;
            }
        }

        /// <summary>
        /// Gets InnerQuery.
        /// </summary>
        public IQueryable<T> InnerQuery
        {
            get
            {
                return this.inner;
            }
        }

        /// <summary>
        /// Gets IQueryable.Provider.
        /// </summary>
        IQueryProvider IQueryable.Provider
        {
            get
            {
                return this.provider;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The to string.
        /// </summary>
        /// <returns>
        /// The to string.
        /// </returns>
        public override string ToString()
        {
            return this.inner.ToString();
        }

        #endregion

        #region Implemented Interfaces

        #region IEnumerable

        /// <summary>
        /// The interface enumerable. get enumerator.
        /// </summary>
        /// <returns>
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.inner.GetEnumerator();
        }

        #endregion

        #region IEnumerable<T>

        /// <summary>
        /// The get enumerator.
        /// </summary>
        /// <returns>
        /// </returns>
        public IEnumerator<T> GetEnumerator()
        {
            return this.inner.GetEnumerator();
        }

        #endregion

        #endregion
    }
}